//
//  CJCustomChoosTypeTableViewCell.m
//  DatangLife
//
//  Created by 陈建 on 2018/5/7.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomChoosTypeTableViewCell.h"

@interface CJCustomChoosTypeTableViewCell ()
{
    UILabel *_typeNameLable;//类型名
    UIView *_typeView;//装载所有属性button的视图
}


@end


@implementation CJCustomChoosTypeTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = kWhiteColor;
        
        _typeNameLable = [[UILabel alloc] initWithFrame:CGRectMake(kSize(15), kSize(10), kSize(200), kSize(20))];
        _typeNameLable.textColor = [UIColor colorWithHexString:kDetailTextBlackColor];
        _typeNameLable.font = kFont(14.f);
        [self addSubview:_typeNameLable];
        
        _typeView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 20)];
        [self addSubview:_typeView];
        
    }
    return self;
}

- (CGFloat)setData:(CJCustomGoodsTypeModel *)model{
    _model = model;
    _typeNameLable.text = model.typeName;
    return [self initTypeView];//每次刷新重绘
}

- (CGFloat)initTypeView{
    //清除typeView 的所有子视图，防止错乱
    while ([_typeView.subviews lastObject] != nil) {
        [[_typeView.subviews lastObject] removeFromSuperview];
    }
    CGFloat lefmagin = kSize(15);
    
    CGFloat upX = kSize(15);
    CGFloat upY = 0;
    NSLog(@"%@",_model.typeArray);
    for (NSInteger i = 0; i < _model.typeArray.count; i++) {
        UIButton *btn = [CJUIKit buttonWithBackgroundColor:kWhiteColor titleColorForNormal:[UIColor colorWithHexString:kNomalTextBlackColor] titleForNormal:[_model.typeArray objectAtIndex:i] titleForSelete:[_model.typeArray objectAtIndex:i] titleColorForSelete:kWhiteColor fontSize:13.f font:nil];
        NSDictionary *dic = [NSDictionary dictionaryWithObject:btn.titleLabel.font forKey:NSFontAttributeName];
        CGSize size = [_model.typeArray[i] sizeWithAttributes:dic];
        
        if (upX > (kScreenWidth - 2*lefmagin - size.width)) {
            upX = lefmagin;
            upY += 30;
        }
        
        btn.frame = CGRectMake(upX, upY, size.width + 30, 24);
        
        [CJUIKit viewcornerRadius:4 andColor:[UIColor colorWithHexString:kDetailTextGrayBlackColor] andWidth:0.5 onView:btn];
        [_typeView addSubview:btn];
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(touchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        upX += size.width + 40;
        if (_model.selectIndex == i) {
            btn.selected = YES;
            btn.backgroundColor = kCommonRedColor;
        }
    }
    upY += 30;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(lefmagin, upY + 10, kScreenWidth - 2* lefmagin, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHexString:kLineViewBgColor];
    [_typeView addSubview:lineView];
    
    _typeView.frame = CGRectMake(0, 40, kScreenWidth, upY + 11);
    return upY + 11 + 40;
    
}


- (void)touchBtnClick:(UIButton *)btn{
    if (btn.selected == NO) {
        _model.selectIndex = (NSInteger)btn.tag-100;
    }else
    {
        //取消选中
        _model.selectIndex = -1;
    }
    if (self.selectButton) {
        self.selectButton(_model.selectIndex);
    }
    for (int i = 0; i<_model.typeArray.count; i++) {
        UIButton *button =(UIButton *)[self viewWithTag:100+i];
        button.selected = NO;
        [button setBackgroundColor:kWhiteColor];
        //根据seletIndex 确定用户当前点了那个按钮
        if (_model.selectIndex == i) {
            button.selected = YES;
            button.backgroundColor = kCommonRedColor;
        }
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
 
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
